
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 3 **********************************
******* THIS FILE GENERATES THE CONDITIONAL DENIAL PROBABILITIES ***************
********************************************************************************
********************************************************************************

*Version: 2023 March 29


cap mkdir `"$output/log/3_denial_probabilities"'
cap mkdir `"$output/temp"'

cap log close 
log using `"$output/log/3_denial_probabilities/3_denial_probabilities.smcl"', replace 

di "This run uses code version from 2023/03/29"

use `"$output/data/$line_file"', clear

*select sample first instance 
keep if R_1 == 1 & Y_0 == 0 			// Keep if at least one resubmission 
assert Y_0 == 0 

gen d_1 = 1-Y_1
gen d_2 = 1-Y_2
gen d_3 = 1-Y_3
gen d_4 = 1-Y_4
gen d_5 = 1-Y_5

//generate bins, bin claim amounts - width of bin = USD 25
gegen bin_C = cut(line_item_value) , at(0(25)250)	
replace bin_C = 250 if bin_C == . 	

*****************************************************************************************
*** Calculate denial probabilities for every procedure and save data to re-merge later***
*****************************************************************************************

// Conditional probabilty that the first resubmission is paid
preserve 

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_1)  	
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "1"
		
	save `"$output/temp/denial_probabilities_bin_second_submission.dta"', replace 
	
restore 



*select sample second instance 
keep if R_2 == 1 & Y_1 == 0 			 // Keep if at least one resubmission 
assert Y_1 == 0 


*****************************************************************************************
*** Calculate denial probabilities for every procedure and save data to re-merge later***
*****************************************************************************************

// Conditional probabilty that the second resubmission is paid 
preserve 

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_2)
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "2"

	save `"$output/temp/denial_probabilities_bin_third_submission.dta"', replace 
	
restore 


*select sample second instance 
keep if R_3 == 1 & Y_2 == 0 			 // Keep if at least one resubmission 
assert Y_2 == 0 


*****************************************************************************************
*** Calculate denial probabilities for every procedure and save data to re-merge later***
*****************************************************************************************

// Conditional probabilty that the third resubmission is paid
preserve 

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_3) 		
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "3"
	
	save `"$output/temp/denial_probabilities_bin_fourth_submission.dta"', replace 
	
restore 



*select sample second instance 
keep if R_4 == 1 & Y_3 == 0 			 // Keep if at least one resubmission 
assert Y_3 == 0 


*****************************************************************************************
*** Calculate denial probabilities for every procedure and save data to re-merge later***
*****************************************************************************************

// Conditional probabilty that the fourth resubmission is paid
preserve 

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_4)  	
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "4"
	
	save `"$output/temp/denial_probabilities_bin_fifth_submission.dta"', replace 
	
restore 


*select sample second instance 
keep if R_4 == 1 & Y_3 == 0 			 // Keep if at least one resubmission 
assert Y_3 == 0 


*****************************************************************************************
*** Calculate denial probabilities for every procedure and save data to re-merge later***
*****************************************************************************************

// Conditional probabilty that the fifth resubmission is paid
preserve 

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_5) 	
	
	keep state pay_type_pooled visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_pooled visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_pooled visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "5"
	
	save `"$output/temp/denial_probabilities_bin_sixth_submission.dta"', replace 
	
restore 



********Generate means

** Bin 
use `"$output/temp/denial_probabilities_bin_second_submission.dta"', clear
append using `"$output/temp/denial_probabilities_bin_third_submission.dta"'
append using `"$output/temp/denial_probabilities_bin_fourth_submission.dta"'
append using `"$output/temp/denial_probabilities_bin_fifth_submission.dta"'
append using `"$output/temp/denial_probabilities_bin_sixth_submission.dta"'

bys state pay_type_pooled visit_code_cat bin size_indicator: gegen mean_p_denial = mean(p_denial) [aweight=observations]

bys state pay_type_pooled visit_code_cat bin size_indicator: gegen total_observations = sum(observations)

sort state pay_type_pooled visit_code_cat bin size_indicator resubmission
by state pay_type_pooled visit_code_cat bin size_indicator: gen resubmissions_used_str = ///
	resubmission[1] + resubmission[2] + resubmission[3] + resubmission[4] + resubmission[5]
	
bys state pay_type_pooled visit_code_cat bin size_indicator: keep if _n == 1 

gen resubmissions_used = strlen(resubmissions_used_str) 
keep state pay_type_pooled visit_code_cat mean_p_denial total_observations resubmissions_used bin size_indicator
rename mean_p_denial p_denial
save `"$output/temp/mean_denial_probabilities_bin.dta"', replace 

erase `"$output/temp/denial_probabilities_bin_second_submission.dta"'
erase `"$output/temp/denial_probabilities_bin_third_submission.dta"'
erase `"$output/temp/denial_probabilities_bin_fourth_submission.dta"'
erase `"$output/temp/denial_probabilities_bin_fifth_submission.dta"'
erase `"$output/temp/denial_probabilities_bin_sixth_submission.dta"'

log close 


